package com.gdut.wifidemo.utils;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;

import com.gdut.wifidemo.common.RequestCallback;
import com.gdut.wifidemo.data.entity.Wifi;

import java.util.ArrayList;
import java.util.List;

public class WifiUtils {

    private static Handler mMainHandler = new Handler(Looper.getMainLooper());

    @TargetApi(23)
    public static void getSearchWifiList(Context context, final RequestCallback<List<Wifi>> callback) {
        final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            int state = wifiManager.getWifiState();
            if (state == WifiManager.WIFI_STATE_DISABLED || state == WifiManager.WIFI_STATE_UNKNOWN) {
                wifiManager.setWifiEnabled(true);
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    wifiManager.startScan();
                    int wifiListSize = 0;
                    int time = 0;
                    int collect = 0;
                    final List<Wifi> wifiList = new ArrayList<>();
                    do {
                        final List<ScanResult> results = wifiManager.getScanResults();
                        if (results != null) {
                            wifiListSize = results.size();
                        }
                        if (wifiListSize == 0) {
                            LogUtils.d("wifiListSize == 0, sleep 1000ms");
                            SystemClock.sleep(1000);
                        }
                        if (time++ > 50) {
                            LogUtils.e("Scan Wifi Timeout");
                            mMainHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (callback != null) {
                                        callback.onLog("Scan Wifi Timeout");
                                        callback.onFailure("Scan Wifi Timeout");
                                    }
                                }
                            });
                            return;
                        } else {
                            if (time > 1) {
                                final int times = time - 1;
                                LogUtils.d("Retry times = " + times);
                                mMainHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (callback != null) {
                                            callback.onLog("Retry times = " + times);
                                        }
                                    }
                                });
                            }
                        }
                        if (results != null && wifiListSize > 0) {
                            for (ScanResult result : results) {
                                Wifi wifi = new Wifi.Builder()
                                        .bssid(result.BSSID)
                                        .ssid(result.SSID)
                                        .rssi(result.level)
                                        .venueName((String) result.venueName)
                                        .build();
                                wifiList.add(wifi);
                            }
                            time = 0;
                            final int times = ++collect;
                            mMainHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (callback != null) {
                                        for (Wifi wifi : wifiList) {
                                            callback.onLog(wifi.toString());
                                        }
                                        if (times >= 5) {
                                            callback.onSuccess(wifiList);
                                        }
                                    }
                                }
                            });
                        }
                    } while (wifiList.size() == 0 || collect < 5);
                }
            }).start();
        }
    }

}
